﻿using Anke.SHManage.Model.EasyUIModel;
using Anke.SHManage.Model.FixedAssetsModel;
using Anke.SHManage.MSSQLDAL.FixedAssetsDAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Anke.SHManage.BLL.FixedAssetsBLL
{
    /// <summary>
    /// 固定资产仓库维护 BLL层
    /// plq
    /// </summary>
    public class FixedAssetsStorageBLL
    {
        /// <summary>
        /// 获取所有固定资产仓库-不筛选是否有效
        /// </summary>
        /// <returns></returns>
        public List<FixedAssetsStorageInfo> GetAllStorageList()
        {
            return new FixedAssetsStorageDAL().GetAllStorageList();
        }

        /// <summary>
        /// 获取有效的固定资产仓库
        /// </summary>
        /// <returns></returns>
        public List<FixedAssetsStorageInfo> GetActiveAssetsStorageList()
        {
            return new FixedAssetsStorageDAL().GetActiveAssetsStorageList();
        }

        #region list集合转easyui 树格式数据集合

        public List<TreeNode> ToTreeNodes(List<FixedAssetsStorageInfo> list)
        {
            List<TreeNode> listNodes = new List<TreeNode>();
            //生成 树节点时，根据 pid=-1的根节点 来生成
            LoadTreeNode(list, listNodes, "-1");
            return listNodes;
        }

        /// <summary>
        /// 递归数据集合 创建 树节点集合
        /// 无级递归
        /// </summary>
        /// <param name="listStorage">数据集合</param>
        /// <param name="listChildren">树节点集合</param>
        /// <param name="pID">节点父id</param>
        public static void LoadTreeNode(List<FixedAssetsStorageInfo> listStorage, List<TreeNode> listChildrenNode, string pID)
        {
            foreach (var storage in listStorage)
            {
                //判断权限ParentID 是否和 传入参数相等
                if (storage.FuJiCangKuBianMa.ToString().Equals(pID))
                {
                    //将 权限转成 树节点
                    TreeNode node = new FixedAssetsStorageBLL().ToTreeNode(storage);
                    //将节点加入到 树子节点集合
                    listChildrenNode.Add(node);

                    //递归 为这个新创建的 树节点找 子节点
                    LoadTreeNode(listStorage, node.children, node.id);
                }
            }
        }

        /// <summary>
        /// 生成TreeNode
        /// </summary>
        /// <returns></returns>
        private TreeNode ToTreeNode(FixedAssetsStorageInfo info)
        {
            //对象的初始化器直接构造
            TreeNode treeNode = new TreeNode()
            {
                id = info.CangKuBianMa.ToString(),
                text = info.CangKuMingCheng,
                state = "open",
                Checked = false,
                attributes = new { IsActive = info.ShiFouYouXiao },
                children = new List<TreeNode>() // 子节点集合
            };

            return treeNode;
        }


        #endregion


        /// <summary>
        /// 添加固定资产仓库
        /// </summary>
        /// <param name="PID"></param>
        /// <param name="sName"></param>
        /// <returns></returns>
        public bool AddAssetsStorage(string PID, string sName)
        {
            return new FixedAssetsStorageDAL().AddAssetsStorage(PID, sName);
        }

        /// <summary>
        /// 修改固定资产仓库名
        /// </summary>
        /// <param name="SID"></param>
        /// <param name="sName"></param>
        /// <returns></returns>
        public bool EditAssetsStorage(string SID, string sName)
        {
            return new FixedAssetsStorageDAL().EditAssetsStorage(SID, sName);
        }

        /// <summary>
        /// 禁用仓库
        /// </summary>
        /// <param name="SID"></param>
        /// <returns></returns>
        public bool DisableAssetsStorage(string SID)
        {
            return new FixedAssetsStorageDAL().DisableAssetsStorage(SID);
        }

        /// <summary>
        /// 启用仓库
        /// </summary>
        /// <param name="SID"></param>
        /// <returns></returns>
        public bool EnableAssetsStorage(string SID)
        {
            return new FixedAssetsStorageDAL().EnableAssetsStorage(SID);
        }

    }
}
